import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import nprogress from 'nprogress'
import { getQueryVariable } from "@/utils/tools";
 
const router = createRouter({
  history: import.meta.env.DEV ? createWebHashHistory() : createWebHistory(),
  routes: [
    {
      path:'/',
      children:[
        {
          path: 'txtReader',
          component: () => import('@reader/views/readPage/txtViewer.vue'),
        },
        {
          path: 'pdfReader',
          component: () => import('@reader/views/readPage/pdfViewer.vue'),
        },
      ],
      redirect:()=>{
        const bookName = getQueryVariable('name');
        const chartIdx = (bookName as string).lastIndexOf('.');
        const format = (bookName as string).slice(chartIdx + 1);
        if (format == 'PDF') {
           return 'pdfReader' 
        } else {
          return 'txtReader'
        }
      },
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('@manager/views/error/404.vue'),
    },
  ],
})
// 路由守卫
router.beforeEach(() => {
  nprogress.start()
})

router.afterEach(() => {
  nprogress.done()
})

export default router
